<template>
  <div
    id="main-container"
    class="main-container"
    :class="$store.state.app.collapse ? 'position-collapse-left' : 'position-left'"
  >
    <!-- 标签页 -->
    <div class="tab-container">
      <el-tabs
        class="tabs"
        :class="$store.state.app.collapse ? 'position-collapse-left' : 'position-left'"
        v-model="mainTabsActiveName"
        :closable="true"
        type="card"
        @tab-click="selectedTabHandle"
        @tab-remove="removeTabHandle"
      >
        <el-dropdown class="tabs-tools" :show-timeout="0" trigger="hover">
          <div style="font-size: 20px; width: 50px">
            <i class="el-icon-arrow-down"></i>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="tabsCloseCurrentHandle">关闭当前标签</el-dropdown-item>
            <el-dropdown-item @click.native="tabsCloseOtherHandle">关闭其它标签</el-dropdown-item>
            <el-dropdown-item @click.native="tabsCloseAllHandle">关闭全部标签</el-dropdown-item>
            <el-dropdown-item @click.native="tabsRefreshCurrentHandle">刷新当前标签</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-tab-pane v-for="item in mainTabs" :key="item.name" :label="item.title" :name="item.name">
          <span slot="label">
            <i :class="item.icon"></i>
            {{ item.title }}
          </span>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 主内容区域 -->
    <div class="main-content">
      <div class="content-box">
        <keep-alive>
          <transition name="fade" mode="out-in">
            <router-view></router-view>
          </transition>
        </keep-alive>
        <first v-if="tab == '首页'"></first>
      </div>

      <div class="content-text">四川师范大学后勤保障部版权所有</div>
    </div>
  </div>
</template>

<script>
import first from '../FirstPage';
export default {
  components: {
    first
  },
  data() {
    return {
      tab: ''
    };
  },
  computed: {
    mainTabs: {
      get() {
        return this.$store.state.tab.mainTabs;
      },
      set(val) {
        this.$store.commit('updateMainTabs', val);
      }
    },
    mainTabsActiveName: {
      get() {
        return this.$store.state.tab.mainTabsActiveName;
      },
      set(val) {
        this.$store.commit('updateMainTabsActiveName', val);
      }
    }
  },
  methods: {
    // tabs, 选中tab
    selectedTabHandle(tab) {
      // console.log(tab);
      tab = this.mainTabs.filter((item) => item.name === tab.name);
      this.tab = tab[0].name;
      console.log(this.tab);
      if (tab.length >= 1) {
        this.$router.push({ name: tab[0].name });
      }
    },
    // tabs, 删除tab
    removeTabHandle(tabName) {
      // console.log("这是啥");
      this.mainTabs = this.mainTabs.filter((item) => item.name !== tabName);
      if (this.mainTabs.length >= 1) {
        // 当前选中tab被删除
        if (tabName === this.mainTabsActiveName) {
          this.$router.push({ name: this.mainTabs[this.mainTabs.length - 1].name }, () => {
            this.mainTabsActiveName = this.$route.name;
          });
        }
      } else {
        this.$router.push('/');
      }
    },
    // tabs, 关闭当前
    tabsCloseCurrentHandle() {
      this.removeTabHandle(this.mainTabsActiveName);
      if (this.mainTabs.length === 0) {
        this.$router.push('/');
      }
    },
    // tabs, 关闭其它
    tabsCloseOtherHandle() {
      this.mainTabs = this.mainTabs.filter((item) => item.name === this.mainTabsActiveName);
    },
    // tabs, 关闭全部
    tabsCloseAllHandle() {
      this.mainTabs = [];
      this.$router.push('/');
    },
    // tabs, 刷新当前
    tabsRefreshCurrentHandle() {
      var tempTabName = this.mainTabsActiveName;
      this.removeTabHandle(tempTabName);
      this.$nextTick(() => {
        this.$router.push({ name: tempTabName });
      });
    }
  }
};
</script>

<style scoped lang="scss">
.main-container {
  padding: 0 5px 5px;
  position: absolute;
  top: 60px;
  left: 1px;
  right: 1px;
  bottom: 0px;
  background: #f2f6fc; //改变内容的颜色
  // background: rgb(3,24,109);
  .tab-container {
    color: #fff;
  }
  .tabs {
    position: fixed;
    top: 60px;
    right: 50px;
    padding-left: 0px;
    padding-right: 2px;
    z-index: 1020;
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    background-color: #17234a;
    color: #ccc;

    border-bottom-width: 1px;
    border-bottom-style: solid;
    /deep/.el-tabs__item {
      padding: 0 20px;
      height: 40px;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      line-height: 40px;
      display: inline-block;
      list-style: none;
      font-size: 14px;
    }
  }
  .tabs-tools {
    position: fixed;
    top: 60px;
    right: 0;
    z-index: 1020;
    height: 40px;
    font-size: 14px;
    line-height: 40px;
    cursor: pointer;
    background-color: #17234a;
    color: #fff;
  }
  .tabs-tools:hover {
    color: #2f9ee2;
  }
  .main-content {
    position: absolute;
    top: 45px;
    left: 5px;
    right: 5px;
    bottom: 5px;
    padding: 5px;

    .content-box {
      height: calc(100% - 25px);
      overflow-y: auto;
    }

    .content-text {
      font-size: 14px;
      color: #666666;
      text-align: center;
      padding: 5px 0;
    }
  }
}
.position-left {
  left: 200px;
}
.position-collapse-left {
  left: 65px;
}
</style>
